<%@page import="org.apache.jasper.tagplugins.jstl.core.Redirect"%>
<%@page import="classes.Customer"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Totalitea - Registration</title>
		<jsp:useBean id="registrationBean" class="beans.RegistrationBean" scope="page"></jsp:useBean>
		<jsp:useBean id="searchBean" class="beans.ProductBeanDAO" scope="page"></jsp:useBean>
		<meta name="tea" content="All the tea!">
		<link rel="stylesheet" href="main.css" type="text/css">
	</head>
	<body>
		<img class="banner" src="images/TotaliteaLogo.png">
        
        <ul class="navigationBar">
	        <li class="navigationBarList"><a id="activePage" class="navigationBarLink" href="index.jsp">Home</a></li>
	        <li class="navigationBarList"><a class="navigationBarLink" href="search.jsp">Search</a></li>
	        <li class="navigationBarList"><a class="navigationBarLink" href="basket.jsp">Basket</a></li>
	        <li class="navigationBarList"><a class="navigationBarLink" href="stockReport.jsp">Stock Report</a></li>
        </ul>
	        
	    <div class="searchForm">
	            <form action="ResultsServlet" METHOD="GET" name="resultsPageForm1">
	            	<input type="hidden" name="search" value="1">
	                Search: <input type="search" name="searchQuery">
	                <input type="submit" value="Search">
	            </form>
	    </div>
        
        <h1>Welcome to Totali<font color="#B5E61D">tea</font></h1>
        <h2 align="center">Registration</h2>
        
        <form action=RegistrationServlet METHOD=POST name=registerNewUser>
	        <table align="center">
	        	<tr>
	        		<td> First Name </td>
	        		<td> <input type="text" name="firstName"> </td>
	        	</tr>
	        	<tr>
	        		<td> Last Name </td>
	        		<td> <input type="text" name="lastName"> </td>
	        	</tr>
	        	<tr>
	        		<td> Address Line 1 </td>
	        		<td> <input type="text" name="addressLine1"> </td>
	        	</tr>
	        	<tr>
	        		<td> City/Town </td>
	        		<td> <input type="text" name="addressLine2"> </td>
	        	</tr>
	        	<tr>
	        		<td> County </td>
	        		<td> <input type="text" name="addressLine3"> </td>
	        	</tr>
	        	<tr>
	        		<td> Postcode </td>
	        		<td> <input type="text" name="postcode"> </td>
	        	</tr>
	        	<tr>
	        		<td> Date of Birth YYYY-MM-DD </td>
	        		<td> <input type="text" name="birthDate"> </td>
	        	</tr>
	        	<tr>
	        		<td> Email Address </td>
	        		<td> <input type="text" name="emailAddress"> </td>
	        	</tr>
	        	<tr>
	        		<td> Username </td>
	        		<td> <input type="text" name="username"> </td>
	        	</tr>
	        	<tr>
	        		<td> Password </td>
	        		<td> <input type="password" name="password"> </td>
	        	</tr>
	        	<tr>
	        		<td></td>
	        		<td> <input type="submit" value="Register"> </td>
	        	</tr>
	        </table>
	        
	        <% String firstName = (String) request.getSession().getAttribute("firstName"); %>
			<% String lastName = (String) request.getSession().getAttribute("lastName"); %>
			<% String addressLine1 = (String) request.getSession().getAttribute("addressLine1"); %>
			<% String addressLine2 = (String) request.getSession().getAttribute("addressLine2"); %>
			<% String addressLine3 = (String) request.getSession().getAttribute("addressLine3"); %>
			<% String postcode = (String) request.getSession().getAttribute("postcode"); %>
			<% String birthDate = (String) request.getSession().getAttribute("birthDate"); %>
			<% String emailAddress = (String) request.getSession().getAttribute("emailAddress"); %>
	        <% String username = (String) request.getSession().getAttribute("username"); %>
			<% String password = (String) request.getSession().getAttribute("password"); %>
			 <!-- Register new user using details filled in by user -->
			<% 
			if(registrationBean.doesUsernameAlreadyExist(username) == true)
			{%>
				<script type="text/javascript">
					window.alert("Username already exists");
				</script><%
			}
			else
			{
				if (username != null && password != null)
				{
					if (firstName != "" && lastName != "" && addressLine1 != "" && addressLine2 != "" && 
							addressLine3 != "" && postcode != "" && birthDate != "" && emailAddress != "" && username != "" && password != "")
					{
						registrationBean.registerNewUser(firstName, lastName, addressLine1, addressLine2, addressLine3, postcode, birthDate, emailAddress, username, password);
						%>
						<script type="text/javascript">
							window.alert("Thank you for registering.");
							window.location.href("index.jsp");
						</script>
						<%
					}
					else
					{
						%>
						<script type="text/javascript">
							window.alert("Empty fields!");
						</script>
						<%
					}
				}
			}%>
       	</form>
        
        
	</body>
</html>